package com.ruoyi.project.business.punch.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.model.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.business.punch.entity.ScheduleClockPunch;
import com.ruoyi.project.business.punch.request.AddClockPunchRequest;
import com.ruoyi.project.business.punch.service.IScheduleClockPunchService;
import com.ruoyi.project.system.entity.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 打卡Controller
 * 
 * @author thanks
 * @date 2024-02-05
 */
@Slf4j
@RestController
@RequestMapping("/business/punch")
public class ScheduleClockPunchController extends BaseController {
    @Autowired
    private IScheduleClockPunchService scheduleClockPunchService;

    /**
     * 查询打卡列表
     */
    @PreAuthorize("@ss.hasPermi('business:punch:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScheduleClockPunch scheduleClockPunch) {
        startPage();
        List<ScheduleClockPunch> list = scheduleClockPunchService.selectScheduleClockPunchList(scheduleClockPunch);
        return getDataTable(list);
    }

    /**
     * 导出打卡列表
     */
    @PreAuthorize("@ss.hasPermi('business:punch:export')")
    @Log(title = "打卡", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ScheduleClockPunch scheduleClockPunch) {
        List<ScheduleClockPunch> list = scheduleClockPunchService.selectScheduleClockPunchList(scheduleClockPunch);
        ExcelUtil<ScheduleClockPunch> util = new ExcelUtil<ScheduleClockPunch>(ScheduleClockPunch.class);
        return util.exportExcel(list, "punch");
    }

    /**
     * 获取打卡详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:punch:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(scheduleClockPunchService.selectScheduleClockPunchById(id));
    }

    /**
     * 获取打卡详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:punch:query')")
    @GetMapping(value = "/query-by-date/{date}")
    public AjaxResult queryByDate(@PathVariable("date") String date) {
        log.info("query-by-date {}", date);
        Long userId = getUserId();
        Long tenantId = getTenantId();
        LocalDate localDate = LocalDate.parse(date);
        ScheduleClockPunch res = scheduleClockPunchService.queryByDate(localDate, userId, tenantId);
        return AjaxResult.success(res);
    }

    /**
     * 新增打卡
     */
    @PreAuthorize("@ss.hasPermi('business:punch:add')")
    @Log(title = "打卡", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AddClockPunchRequest request) {
        Long tenantId = getTenantId();
        SysUser user = SecurityUtils.getLoginUser().getUser();
//        log.info("user, {}", JSON.toJSONString(user));
        return toAjax(scheduleClockPunchService.punchClock(request, user, tenantId));
    }

    /**
     * 修改打卡
     */
    @PreAuthorize("@ss.hasPermi('business:punch:edit')")
    @Log(title = "打卡", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScheduleClockPunch scheduleClockPunch) {
        return toAjax(scheduleClockPunchService.updateScheduleClockPunch(scheduleClockPunch));
    }

    /**
     * 删除打卡
     */
    @PreAuthorize("@ss.hasPermi('business:punch:remove')")
    @Log(title = "打卡", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(scheduleClockPunchService.deleteScheduleClockPunchByIds(ids));
    }
}
